# -*-coding:Utf-8 -*
""" GUI Screen class
"""
import bge, bgl, blf
from .color import *
from .font import *
from .image import *
from ..common import *

class Screen(object):
	def __init__(self, width, height):
		self._x = width
		self._y = height
		self.childs = ChildList(self)
		self.layers = {"default":self.childs._list}
		self._sav_layer = None
		self.slectedlayers = ("default",)
		self.screen = (width,height)
		self.lastscreen = (width,height)
		self.minimum = (800,600)
		self._active_hover = True
		
	@property
	def x(self):
		return self._x
	@x.setter	
	def x(self, value):
		ratio = 1.0
		if not self._x == 0:
			ratio = value/self._x
		self._x = value
		for aLayer in self.layers:
			for aChild in self.layers[aLayer]:
				aChild.set_size_x(value,ratio)
	@property
	def w(self):
		return self._x
	
	@property
	def y(self):
		return self._y
	@y.setter	
	def y(self, value):
		ratio = 1.0
		if not self._y == 0:
			ratio = value/self._y
		self._y = value
		for aLayer in self.layers:
			for aChild in self.layers[aLayer]:
				aChild.set_size_y(value,ratio)
	@property
	def h(self):
		return self._y
	
	def addChild(self, value):
		self.layers[self.slectedlayers[-1]].append(value)
		self.childs._list.append(value)
		
	def delChild(self,value):
		if value in self.childs._list:
			#print(value)
			self.childs._list.remove(value)
		
	def setLayer(self,*layers):
		childlist = []
		for name in layers:
			if name in self.layers:
				childlist += self.layers[name]
			else:
				self.layers[name] = []
		self.childs._list = childlist
		self.slectedlayers = layers
		
	def hideLayer(self,*layers):
		active = []
		for name in self.slectedlayers:
			if name not in layers:
				active.append(name)
		self.setLayer(*active)
		
	def showLayer(self,*layers):
		active = list(self.slectedlayers)
		for name in layers:
			if name not in self.slectedlayers:
				active.append(name)
		self.setLayer(*active)
	
	def saveLayer(self):
		self._sav_layer = self.slectedlayers
	def restoreLayer(self):
		if self._sav_layer != None:
			self.slectedlayers = self._sav_layer
			self._sav_layer = False
			
	def update_mouse(self,mousex,mousey,mousesens):
		if not self.check_hover(mousex,mousey):
			return
		if mousesens.getButtonStatus(bge.events.LEFTMOUSE) == bge.logic.KX_INPUT_JUST_RELEASED:
			self.hover.onClic()
		if mousesens.getButtonStatus(bge.events.RIGHTMOUSE) == bge.logic.KX_INPUT_JUST_RELEASED:
			self.hover.onRightClic()
		if mousesens.getButtonStatus(bge.events.MIDDLEMOUSE) == bge.logic.KX_INPUT_JUST_RELEASED:
			self.hover.onWheel()
		if bge.logic.mouse.events[bge.events.WHEELUPMOUSE] == bge.logic.KX_INPUT_JUST_RELEASED:
			self.hover.onWheelUp()
		if bge.logic.mouse.events[bge.events.WHEELDOWNMOUSE] == bge.logic.KX_INPUT_JUST_RELEASED:
			self.hover.onWheelDown()
			
	def check_hover(self,mousex,mousey):
		self.hover = None
		for aChild in self.childs:
			self.hover = aChild.isHover(mousex,mousey, 0,0,self.hover)
		if self.hover != None and self._active_hover:
			self.hover.setHover()
			return True
		return False
		
	def stopMouse(self):
		self._active_hover = False
	def activateMouse(self):
		self._active_hover = True
		
	def setHover(self):
		return
		
	def getAlpha(self):
		return 1.0
		
	def draw(self):
		screenSize = (bge.render.getWindowWidth(),bge.render.getWindowHeight())
		if (screenSize[0] != self.screen[0] or screenSize[1] !=  self.screen[1]) and (screenSize == self.lastscreen) :
			if screenSize[0] != self.screen[0]:
				if screenSize[0] >= self.minimum[0]:
					self.x = screenSize[0]
				elif self.x != self.minimum[0]:
					self.x = self.minimum[0]
			if screenSize[1] !=  self.screen[1]:
				if screenSize[1] >= self.minimum[1]:
					self.y = screenSize[1]
				elif self.y != self.minimum[1]:
					self.y = self.minimum[1]
			self.screen = screenSize
		# Paramétrage d'openGL
		bgl.glDisable(bgl.GL_AUTO_NORMAL)
		bgl.glDisable(bgl.GL_DEPTH_TEST)
		bgl.glDisable(bgl.GL_COLOR_MATERIAL)
		bgl.glDisable(bgl.GL_LIGHTING)
		bgl.glDisable(bgl.GL_CULL_FACE)
		bgl.glDisable(bgl.GL_FOG)
		bgl.glDisable(bgl.GL_SCISSOR_TEST)
		bgl.glEnable(bgl.GL_TEXTURE_2D)
		bgl.glEnable(bgl.GL_BLEND)
		bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)
		bgl.glShadeModel(bgl.GL_SMOOTH)
		bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
		bgl.glMatrixMode(bgl.GL_PROJECTION)
		bgl.glLoadIdentity()
		bgl.gluOrtho2D(0,self.screen[0],0,self.screen[1])
		bgl.glMatrixMode(bgl.GL_MODELVIEW)
		bgl.glLoadIdentity()
		bgl.glColor4f(1.0,1.0,1.0,1.0)
		for aChild in self.childs:
			aChild.draw(0,0,False)
		self.lastscreen = screenSize
"""
class Screen(object):
	
	def __init__(self,width,height,mouse):
		x = ZoneValue(self, 0)
		y = ZoneValue(self, 0)
		width = ZoneValue(self, width)
		width._auto_offset_value = "x"
		height = ZoneValue(self, height)
		height._auto_offset_value = "y"
		self.childs = ChildList(self)
		
	def render(self,width=self.zone[2],height=self.zone[3]):
		self.zone = (0,0,width,height)
		pyDispCode = {}
		for aCh in self._childs:
			aCh.render(self.zone,pyDispCode)
		
	def display(self):
		# Paramétrage d'openGL
		bgl.glDisable(bgl.GL_AUTO_NORMAL)
		bgl.glDisable(bgl.GL_DEPTH_TEST)
		bgl.glDisable(bgl.GL_COLOR_MATERIAL)
		bgl.glDisable(bgl.GL_LIGHTING)
		bgl.glDisable(bgl.GL_CULL_FACE)
		bgl.glDisable(bgl.GL_FOG)
		bgl.glDisable(bgl.GL_SCISSOR_TEST)
		bgl.glEnable(bgl.GL_TEXTURE_2D)
		bgl.glEnable(bgl.GL_BLEND)
		bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)
		bgl.glShadeModel(bgl.GL_SMOOTH)
		bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
		bgl.glMatrixMode(bgl.GL_PROJECTION)
		bgl.glLoadIdentity()
		bgl.gluOrtho2D(self.zone[0],self.zone[2],self.zone[1],self.zone[3])
		bgl.glMatrixMode(bgl.GL_MODELVIEW)
		bgl.glLoadIdentity()
		bgl.glColor4f(1.0,1.0,1.0,1.0)
		for anOb in self._display.keys():
			if self._display[anOb][0]:
				self._local_var['self'] = anOb
				exec(self._display[anOb][1],globals,self._local_var)
				
	
"""			
			
